<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台分类管理控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class CategoryController extends AdminController {

    /**
     * 分类管理列表
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
        $tree = D('Category')->getTree(0,'id,name,title,sort,pid,allow_publish,status');
        $tre = D('Category')->find();
        $this->assign('tree', $tree);
        C('_SYS_GET_CATEGORY_TREE_', true); //标记系统获取分类树模板
        $this->meta_title = '分类管理';
        $this->display();
    }

    /**
     * 显示分类树，仅支持内部调
     * @param  array $tree 分类树
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function tree($tree = null){
        C('_SYS_GET_CATEGORY_TREE_') || $this->_empty();
        $this->assign('tree', $tree);
        $this->display('tree');
    }

    /* 编辑分类 */
    public function edit($id = null, $pid = 0){
        $Category = D('Category');

        if(IS_POST){ //提交表单
            if(false !== $Category->update()){
                $this->success('编辑成功！', U('index'));
            } else {
                $error = $Category->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {
            $cate = '';
            if($pid){
                /* 获取上级分类信息 */
                $cate = $Category->info($pid, 'id,name,title,status');
                if(!($cate && 1 == $cate['status'])){
                    $this->error('指定的上级分类不存在或被禁用！');
                }
            }

            /* 获取分类信息 */
            $info = $id ? $Category->info($id) : '';

            $this->assign('info',       $info);
            $this->assign('category',   $cate);
            $this->meta_title = '编辑分类';
            $this->display();
        }
    }

    /* 新增分类 */
    public function add($pid = 0){
        $Category = D('Category');

        if(IS_POST){ //提交表单
            if(false !== $Category->update()){
                $this->success('新增成功！', U('index'));
            } else {
                $error = $Category->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {
            $cate = array();
            if($pid){
                /* 获取上级分类信息 */
                $cate = $Category->info($pid, 'id,name,title,status');
                if(!($cate && 1 == $cate['status'])){
                    $this->error('指定的上级分类不存在或被禁用！');
                }
            }

            /* 获取分类信息 */
            $this->assign('category', $cate);
            $this->meta_title = '新增分类';
            $this->display('edit');
        }
    }

    /**
     * 删除一个分类
     * @author huajie <banhuajie@163.com>
     */
    public function remove(){
        $cate_id = I('id');
        if(empty($cate_id)){
            $this->error('参数错误!');
        }

        //判断该分类下有没有子分类，有则不允许删除
        $child = M('Category')->where(array('pid'=>$cate_id))->field('id')->select();
        if(!empty($child)){
            $this->error('请先删除该分类下的子分类');
        }

        //判断该分类下有没有内容
        $document_list = M('Document')->where(array('category_id'=>$cate_id))->field('id')->select();
        if(!empty($document_list)){
            $this->error('请先删除该分类下的文章（包含回收站）');
        }

        //删除该分类信息
        $res = M('Category')->delete($cate_id);
        if($res !== false){
            //记录行为
            action_log('update_category', 'category', $cate_id, UID);
            $this->success('删除分类成功！');
        }else{
            $this->error('删除分类失败！');
        }
    }

    /**
     * 操作分类初始化
     * @param string $type
     * @author huajie <banhuajie@163.com>
     */
    public function operate($type = 'move'){
        //检查操作参数
        if(strcmp($type, 'move') == 0){
            $operate = '移动';
        }elseif(strcmp($type, 'merge') == 0){
            $operate = '合并';
        }else{
            $this->error('参数错误！');
        }
        $from = intval(I('get.from'));
        empty($from) && $this->error('参数错误！');

        //获取分类
        $map = array('status'=>1, 'id'=>array('neq', $from));
        $list = M('Category')->where($map)->field('id,title')->select();

        $this->assign('type', $type);
        $this->assign('operate', $operate);
        $this->assign('from', $from);
        $this->assign('list', $list);
        $this->meta_title = $operate.'分类';
        $this->display();
    }

    /**
     * 移动分类
     * @author huajie <banhuajie@163.com>
     */
    public function move(){
        $to = I('post.to');
        $from = I('post.from');
        $res = M('Category')->where(array('id'=>$from))->setField('pid', $to);
        if($res !== false){
            $this->success('分类移动成功！', U('index'));
        }else{
            $this->error('分类移动失败！');
        }
    }

    /**
     * 合并分类
     * @author huajie <banhuajie@163.com>
     */
    public function merge(){
        $to = I('post.to');
        $from = I('post.from');
        $Model = M('Category');

        //检查分类绑定的模型
        $from_models = explode(',', $Model->getFieldById($from, 'model'));
        $to_models = explode(',', $Model->getFieldById($to, 'model'));
        foreach ($from_models as $value){
            if(!in_array($value, $to_models)){
                $this->error('请给目标分类绑定' . get_document_model($value, 'title') . '模型');
            }
        }
        //检查分类选择的文档类型
        $from_types = explode(',', $Model->getFieldById($from, 'type'));
        $to_types = explode(',', $Model->getFieldById($to, 'type'));
        foreach ($from_types as $value){
            if(!in_array($value, $to_types)){
                $types = C('DOCUMENT_MODEL_TYPE');
                $this->error('请给目标分类绑定文档类型：' . $types[$value]);
            }
        }
        //合并文档
        $res = M('Document')->where(array('category_id'=>$from))->setField('category_id', $to);

        if($res){
            //删除被合并的分类
            $Model->delete($from);
            $this->success('合并分类成功！', U('index'));
        }else{
            $this->error('合并分类失败！');
        }
    }
    public function shop(){
       $name       =   I('name');
        $map['status']  =   array('egt',0);
        if(is_numeric($name)){
            $map['id|name']=   array(intval($name),array('like','%'.$name.'%'),'_multi'=>true);
        }else{
            $map['name']    =   array('like', '%'.(string)$name.'%');
        }

        $list   = $this->lists('shop', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '用户信息';
        $this->display();
    }
    public function addshop(){
        if(IS_POST){
            $friend=D('Shop');
            $upload = new \Think\Upload();//实例化上传类
            $upload->exts =array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath ='./Public/Upload/'; // 设置附件上传目录
            $info = $upload->uploadOne($_FILES['pic']);
            if($info) {// 上传错误提示错误信息
            $_POST['pic']=$info['savepath'].$info['savename'];
    }else{
           $this->error($upload->getError()); 
        }
            $pid=$res['cate'];
            $a=M('category')->where('id='.$pid)->find();
            $res['cate']=$a['title'];
            $res=$friend->create();
            if($res){
                if($friend->add($res)){
                    $this->success('添加成功',U('shop'));
                }else{
                    $this->error('添加失败',U('add'));
                }

            }else{
                $this->error($friend->getError());
            }
        }else{
            $cate = M('category')->where(array('pid'=>0))->select();
            $this->assign('cate',$cate );
            $this->meta_title = '添加商品';
            $this->display();
        }
    }
    public function shopupdate(){
        if (IS_POST) {
            $friend=D('Shop');
            $upload = new \Think\Upload();//实例化上传类
            $upload->exts =array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath ='./Public/Upload/'; // 设置附件上传目录
            $info = $upload->uploadOne($_FILES['pic']);
            if($info!=''){
               if($info) {// 上传错误提示错误信息
                      $_POST['pic']=$info['savepath'].$info['savename'];   
                   }else{
                      $this->error($upload->getError()); 
                 }
            }
            
            $id=I('post.id');
            $_POST['addtime']=time();
            $res=M('shop')->where('id='.$id)->save($_POST);
            if ($res) {
                $this->success('修改成功',U('shop'));
            }else{
                $this->error('添加失败',U('shopupdate',array('id'=>$id)));
            }
        }else{
             $cate = M('category')->where(array('pid'=>0))->select();
             $this->assign('cate',$cate );
             $id=I('get.id');
             $User = M("shop")->find("$id");// 实例化User对象
             $this->assign('User',$User);
             $this->display();
        }
    }
    
//}   
    public function getchild(){
            $id=I('post.id');
            $cate = M('category')->where(array('pid'=>$id))->select();
        if($cate){
            $this->assign('cate',$cate );
            $res['info']=$this->fetch('child');
            $res['msg']='sussecc';
            $res['status']=1;
        }else{
            $res['info']=null;
            $res['msg']='没有更多子集';
            $res['status']=o;
        }
        $this->ajaxReturn($res);
    }
    
     public function delete(){
            $id=I('get.id');
            $a = M('shopatt')->where("pid=$id")->select();
            if($a){
                $this->error('商品存在属性，不可删除',U('shop'));
            }else{
            $res=M('shop')->where("id=$id")->delete($_POST);
            if ($res) {
                $this->success('删除成功',U('shop'));
            }else{
                $this->error('删除失败',U('shop'));
    }    
            }
            
   }
   public function addatt(){
        if (IS_POST) {    
        $res = M('shopatt')->create();
        if ($res) {
         $r=M('shopatt')->add($_POST);
         if ($r) {
          $this->success('添加成功',U('index'));
        }else{
          $this->error('添加失败',U('add'));
        }
        }else{
          $this->error(M('shopatt')->getError());
        }
        }else{
        $a=I('get.id');
        $this->assign('a',$a);
        $this->display();   
        }
    }
    public function att(){
        $id=I('get.id');
        $ser = M("shopatt")->where("id=$id")->find();
        $User = M("att")->where("pid=$id")->select();
        $id=$ser['pid'];
        $er = M("shop")->where("id=$id")->find();
        $this->assign("er",$er);
        $this->assign("ser",$ser);
        $this->assign("User",$User);
        $this->display();
        
    }
    public function addshopatt(){
       if (IS_POST) {
        $res = M('att')->create();
        $a=$_POST;
        if ($res) {
         $r=M('att')->add($_POST);
         if ($r) {
          $this->success('添加成功',U('Shopatt/index'));
        }else{
          $this->error('添加失败',U('Shopatt/index'));
        }
        }else{
          $this->error(M('att')->getError());
        }
        }else{
        $a=I('get.pid');
        $ser = M("shopatt")->where("id=$a")->find();
        $id=$ser['pid'];
        $this->assign('pid',$a);
        $this->assign('id',$id);
        $this->display();   
        }
    }
    public function deletes(){
        $id=I('get.id');
        $res=M('att')->where("id=$id")->delete($_POST);
        if ($res) {
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败',U('index'));
    }
   }
   public function updateshopatt(){
            if (IS_POST) {
            $id=I('post.id'); 
            $res=M('att')->where("id=$id")->save($_POST);
            if ($res) {
                $this->success('修改成功',U('Shopatt/index'));
            }else{
                $this->error('修改失败',U('updateshopatt'));
            }
        }else{
             $id=I('get.id');
             $User = M("att")->find("$id");// 实例化User对象
             $this->assign('User',$User);
             $this->display();
        }
   }
    public function setStatus($model='shop'){
        return parent::setStatus($model);
    }
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        if( in_array(C('USER_ADMINISTRATOR'), $id)){
            $this->error("不允许对超级管理员执行该操作!");
        }
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['uid'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('shop', $map );
                break;
            case 'resumeuser':
                $this->resume('shop', $map );
                break;
            case 'deleteuser':
                $this->delete('shop', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }
}

